//
//  YUIViewController+VCPage.swift
//  EasyEverything/Users/ymac/Downloads/YFree_IOS-master/YFree/Main/ViewController
//
//  Created by YangZhe on 17/1/23.
//  Copyright © 2017年 kuletech. All rights reserved.
//
extension YUIViewController{
    //模态
    open func yPresentVC(storyBoardName: String,storyBoardIdentifier: String,yBundle:[String:Any]? = nil){
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            self.yCloseKeyBoard()
            if yBundle != nil {
                let targetViewController:YUIViewController = UIStoryboard(name: "\(storyBoardName)", bundle: .main).instantiateViewController(withIdentifier:  "\(storyBoardIdentifier)") as! YUIViewController
                targetViewController.yBundle = yBundle!.count > 0 ? yBundle : nil
                let navVC = UINavigationController(rootViewController: targetViewController)
                navVC.modalPresentationStyle = .fullScreen
                self.viewController?.present(navVC, animated: true, completion: nil)
            }else{
                let navVC = UINavigationController(rootViewController: UIStoryboard(name: "\(storyBoardName)", bundle: .main).instantiateViewController(withIdentifier:  "\(storyBoardIdentifier)"))
                navVC.modalPresentationStyle = .fullScreen
                self.viewController?.present(navVC, animated: true, completion: nil)
            }
        })
    }
    
    //压栈
    open func yPushVC(storyBoardName: String,storyBoardIdentifier: String,yBundle:[String:Any]? = nil){
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            self.yCloseKeyBoard()
            if yBundle != nil {
                let targetViewController:YUIViewController = UIStoryboard(name: "\(storyBoardName)", bundle: .main).instantiateViewController(withIdentifier:  "\(storyBoardIdentifier)") as! YUIViewController
                targetViewController.yBundle = yBundle!.count > 0 ? yBundle : nil
                self.viewController?.navigationController?.pushViewController(targetViewController, animated: true)
            }else{
                self.viewController?.navigationController?.pushViewController(UIStoryboard(name: "\(storyBoardName)", bundle: .main).instantiateViewController(withIdentifier:  "\(storyBoardIdentifier)"), animated: true)
            }
        })
    }
    
    open func yDismissVC(animated: Bool = true, _ completion:(()->Void)? = nil){
        yDismissLoadingProgress()
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            self.dismiss(animated: animated, completion: completion)
        })
    }
    
    open func yPopVC(animated: Bool = true) {
        yDismissLoadingProgress()
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            let _ = self.viewController.navigationController?.popViewController(animated: animated)
        })
    }
    
    open func yPopVCWithResult(responseCode:Int,data:Any?){
        yDismissLoadingProgress()
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            if let previousVC = self.viewController.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 2] as? YOnViewControllerResultDelegate{
                previousVC.yOnViewControllerResult(responseCode: responseCode, data: data)
                self.yPopVC()
            }
        })
    }

    open func yPopToTargetVC(viewController: YUIViewController){
        yDismissLoadingProgress()
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            let _ = self.viewController.navigationController?.popToViewController(viewController, animated: true)
        })
    }

    open func yPopToRootVC(){
        yDismissLoadingProgress()
        let _ = self.yRunOnUIThreadDelay(delay: 0.1, block: {
            let _ = self.viewController.navigationController?.popToRootViewController(animated: true)
        })
    }
    
    open func yShowAlertController(alertView:UIAlertController,hasCancelAction:Bool,alertActions:[UIAlertAction]){
        if alertView.title == nil && alertView.message != nil{
            alertView.title = "\(YAppUtils.getAppName())提示"
        }
        for alertAction in alertActions{
            alertView.addAction(alertAction)
        }
        if hasCancelAction{
            alertView.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        }
        alertView.view.tintColor = YConfig.themeColor
        viewController.present(alertView, animated: true, completion: nil)
    }
    
    open func yShowAlertController(alertView:UIAlertController,hasCancelAction:Bool,alertActions:UIAlertAction...){
        yShowAlertController(alertView: alertView, hasCancelAction: hasCancelAction, alertActions: alertActions)
    }
}
